#include <QPushButton>
#include "KeyBoard.h"

const int WIDTH = 18;
const int HEIGHT = 18;

KeyBoard::KeyBoard(QWidget *parent)
    : QWidget(parent, Qt::FramelessWindowHint)
    , m_buttonGrp()
    , m_mainLayout(this)
{
    this->setFixedSize(60, 60);
    initView();
    initConnects();
}

KeyBoard::~KeyBoard()
{
    finalConnects();
}

void KeyBoard::initView()
{
    for (int i = 0; i < 9; ++i) {
        m_keyButton[i].setText(QString("%1").arg(i + 1));
        m_keyButton[i].setParent(this);
        m_keyButton[i].setFixedSize(WIDTH, HEIGHT);
        m_buttonGrp.addButton(&m_keyButton[i], i);
        m_keyButton[i].move((i%3)*(WIDTH + 1) + 1, (i/3)*(HEIGHT + 1) + 1);
    }
}

void KeyBoard::initConnects()
{
    connect(&m_buttonGrp, SIGNAL(buttonClicked(int)), this, SLOT(onButtonGroup(int)));
}

void KeyBoard::finalConnects()
{
    disconnect(&m_buttonGrp, SIGNAL(buttonClicked(int)), this, SLOT(onButtonGroup(int)));
}

void KeyBoard::onButtonGroup(int num)
{
    emit keyBoardResult(num + 1);
    this->close();
}
